package it.dei.unipd.healthservice.resource;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
/**
 * Invia un'email di conferma account ad un nuovo cliente registrato
 * @author Simone Zennaro
 * @version 1.00
 */
public class SendMail

{
	/**
	 * Invia una email
	 * 
	 * @param to
	 *            destinatario
	 * @param sub
	 *            oggetto dell' email
	 * @param msg
	 *            corpo del messaggio
	 * @param user
	 *            username per l'autenticazione a gmail per l'invio dell'email
	 * @param pass
	 *            password per l'autenticazione a gmail per l'invio dell'email
	 */
	public static void send(String to, String sub, String msg,
			final String user, final String pass) {

		// create an instance of Properties Class
		Properties props = new Properties();

		/*
		 * Specifies the IP address of your default mail server for e.g if you
		 * are using gmail server as an email sever you will pass smtp.gmail.com
		 * as value of mail.smtp host. As shown here in the coding. Change
		 * accordingly, if your email id is not an gmail id
		 */
		props.put("mail.smtp.host", "smtp.gmail.com");

		props.put("mail.smtp.port", "587"); // this is optional
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");

		/*
		 * Pass Properties object(props) and Authenticator object for
		 * authentication to Session instance
		 */

		Session session = Session.getInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(user, pass);
					}
				});

		try {

			/*
			 * Create an instance of MimeMessage, it accept MIME types and
			 * headers
			 */

			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(user));
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(
					to));
			message.setSubject(sub);
			message.setText(msg);

			/* Transport class is used to deliver the message to the recipients */
			Transport.send(message);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}